package com.jtech.together.mvp.presenter;

import android.content.Context;
import android.os.Bundle;

import com.jtech.together.manager.UserManager;
import com.jtech.together.model.UserModel;
import com.jtech.together.mvp.contract.RecommendUserContract;
import com.jtech.together.util.BmobUtil;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * 推荐用户
 * Created by wuxubaiyang on 2017/5/10.
 */
public class RecommendUserPresenter implements RecommendUserContract.Presenter {
    private Context context;
    private RecommendUserContract.View view;

    public RecommendUserPresenter(Context context, RecommendUserContract.View view, Bundle bundle) {
        this.context = context;
        this.view = view;
    }

    @Override
    public void loadRecommendUser(int pageIndex, int displayNumber, final boolean loadMore) {
        UserModel userModel = UserManager.getCurrentUser();
        BmobQuery<UserModel> query = new BmobQuery<>();
        //查询最新注册的非当前用户
        query.setLimit(displayNumber)
                .order("-createdAt")
                .setSkip(pageIndex * displayNumber)
                .addWhereNotEqualTo("objectId", userModel.getObjectId())
                .findObjects(new FindListener<UserModel>() {
                    @Override
                    public void done(List<UserModel> list, BmobException e) {
                        if (null == e) {
                            view.loadSuccess(list, loadMore);
                        } else {
                            view.loadFail(BmobUtil.getErrorMessage(e.getErrorCode()));
                        }
                    }
                });
    }
}